/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameUser;

import Database.CheckConnection;
import Database.CreateCache;
import Database.HapusCache;
import FrameSekretariat.SekretariatFrameUtama;
import Model.Dokumen;
import Model.Surat;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author WIN7
 */
public class UserFrameUtama extends JFrame {

    private JMenuBar menuBar;
    private JMenu surat, dokumen, Menu, berkas;
    private JMenuItem lhtsrt, carisrt, caridok, lihatdok, profil, exit;
    private ArrayList<Surat> dataSurat;
    private ArrayList<Dokumen> dataDokumen;

    public UserFrameUtama() throws HeadlessException {
        this.setTitle("User Utama");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setBounds(350, 200, 500, 400);
        this.setLayout(null);

        menuBar = new JMenuBar();
        menuBar.setSize(500, 30);
        this.add(menuBar);

        Menu = new JMenu("Menu");
        Menu.setSize(70, 30);
        menuBar.add(Menu);

        profil = new JMenuItem("Profile User");
        profil.setSize(70, 30);
        Menu.add(profil);
        profil.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UserProfile profile = new UserProfile();
                    profile.setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(UserFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(UserFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        exit = new JMenuItem("Close & Logout");
        exit.setSize(70, 30);
        Menu.add(exit);
        exit.addActionListener(new ActionListener() {

            private String[] args;

            @Override
            public void actionPerformed(ActionEvent e) {
                HapusCache.main(args);
                CreateCache.main(args);
                setVisible(false);
                JOptionPane.showMessageDialog(null, "Anda telah Logout");
            }
        });

        berkas = new JMenu("Berkas");
        berkas.setSize(70, 30);
        menuBar.add(berkas);

        surat = new JMenu("Surat");
        surat.setSize(70, 30);
        berkas.add(surat);

        lhtsrt = new JMenuItem("Daftar Surat");
        lhtsrt.setSize(70, 30);
        surat.add(lhtsrt);
        lhtsrt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dataSurat = new ArrayList<Surat>();
                Connection con = CheckConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "12345");
                Statement stat = null;
                try {
                    stat = con.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(UserFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                String sql = "select * from SURAT where ID_PENERIMA in (select DATA from CACHE)";
                ResultSet hasil = null;
                try {
                    hasil = stat.executeQuery(sql);
                } catch (SQLException ex) {
                    Logger.getLogger(UserFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    while (hasil.next()) {
                        Surat surat = new Surat();
                        try {
                            surat.setNomor_surat(hasil.getString("NO_SURAT"));
                            surat.setPerihal(hasil.getString("PERIHAL"));
                            surat.setAsal_surat(hasil.getString("ASAL_SURAT"));
                            surat.setPenerima_surat(hasil.getString("PENERIMA_SURAT"));
                            surat.setId_penerima(hasil.getString("ID_PENERIMA"));
                            surat.setLetak_surat(hasil.getString("LETAK_SURAT"));
                            surat.setPetugas_penerima(hasil.getString("PETUGAS_PENERIMA"));
                            surat.setTanggal_terima(hasil.getString("TANGGAL_TERIMA"));
                            surat.setTerima(hasil.getString("DAFTAR_TERIMA"));
                            dataSurat.add(surat);
                        } catch (Exception ex) {
                            Logger.getLogger(UserFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(UserFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                UserLihatSurat surat = new UserLihatSurat(dataSurat);
                surat.setVisible(true);
            }
        });

        carisrt = new JMenuItem("Cari Surat");
        carisrt.setSize(70, 30);
        surat.add(carisrt);
        carisrt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                UserCariSurat carisurt = new UserCariSurat();
                carisurt.setVisible(true);
            }
        });

        dokumen = new JMenu("Dokumen");
        dokumen.setSize(70, 30);
        berkas.add(dokumen);

        lihatdok = new JMenuItem("Daftar Dokumen");
        lihatdok.setSize(70, 30);
        dokumen.add(lihatdok);
        lihatdok.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dataDokumen = new ArrayList<Dokumen>();
                Connection con = CheckConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "12345");
                Statement stat = null;
                try {
                    stat = con.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(UserFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                String sql = "select * from DOKUMEN where ID_PENERIMA in (select DATA from CACHE)";
                ResultSet hasil = null;
                try {
                    hasil = stat.executeQuery(sql);
                } catch (SQLException ex) {
                    Logger.getLogger(UserFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    while (hasil.next()) {
                        Dokumen dokumen = new Dokumen();
                        try {
                            dokumen.setNomor_dokumen(hasil.getString("NO_DOKUMEN"));
                            dokumen.setPerihal(hasil.getString("PERIHAL"));
                            dokumen.setAsal_dokumen(hasil.getString("ASAL_DOKUMEN"));
                            dokumen.setPenerima_dokumen(hasil.getString("PENERIMA_DOKUMEN"));
                            dokumen.setId_penerima(hasil.getString("ID_PENERIMA"));
                            dokumen.setLetak_dokumen(hasil.getString("LETAK_DOKUMEN"));
                            dokumen.setPetugas_penerima(hasil.getString("PETUGAS_PENERIMA"));
                            dokumen.setTanggal_terima(hasil.getString("TANGGAL_TERIMA"));
                            dokumen.setTerima(hasil.getString("DAFTAR_TERIMA"));
                            dataDokumen.add(dokumen);
                        } catch (Exception ex) {
                            Logger.getLogger(UserFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(UserFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                UserLihatDokumen dosdok = new UserLihatDokumen(dataDokumen);
                dosdok.setVisible(true);
            }
        });

        caridok = new JMenuItem("Cari Dokumen");
        caridok.setSize(70, 30);
        dokumen.add(caridok);
        caridok.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                UserCariDok cardok = new UserCariDok();
                cardok.setVisible(true);
            }
        });
    }
}
